package com.easycarebn.main.fragment;

import java.util.List;

import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.easycarebn.main.utils.IResult;
import com.easycarebn.main.utils.Utils;
import com.easycarebn.main.view.LineTimeBox;
import com.easycarebn.request.controller.ServerController;
import com.easycarebn.request.data.DoctorScheduleResponse;
import com.easycarebn.request.data.DoctorScheduleResponse.TimeItem;
import com.easycarebn.service.request.OnRequest;
import com.timbacsi.main.R;

public class FragmentCalender {

    private View mView;

    private int mDocterId = 0;
    private List<TimeItem> mList;
    private String mCurrent;
    private String mNext;
    private String mPrev;
    private Activity mActivity;
    private IResult callIResult;
    private IResult mCancelDialog;
    private DoctorScheduleResponse mData;

    public FragmentCalender(Activity activity, IResult callIResult, final int doctorId, List<TimeItem> list,
    		DoctorScheduleResponse data, View view, IResult cancelDialog) {
        mDocterId = doctorId;
        mList = list;
        mCurrent = data.getDate();
        mNext = data.getNext();
        mView = view;
        mActivity = activity;
        mData=data;
        this.callIResult = callIResult;
        this.mCancelDialog = cancelDialog;

    	}

    public void init() {
        LinearLayout lineBoox = (LinearLayout) mView.findViewById(R.id.lineBookKham);
        final TextView tvCurrentDate = (TextView) mView.findViewById(R.id.tv_current_date);
       // tvCurrentDate.setText(DateFormat.format("dd-MM-yyyy", Utils.convertDate(mCurrent, "yyyy-MM-dd")));
       
        tvCurrentDate.setText(mData.getShowdate());
        
        
        final View btnback = mView.findViewById(R.id.btn_back);
        btnback.setTag(mPrev);
        if (TextUtils.isEmpty(mPrev) || "null".equals(mPrev)) {
            btnback.setVisibility(View.INVISIBLE);
        } else {
            btnback.setVisibility(View.VISIBLE);
        }
        final View btnnext = mView.findViewById(R.id.btn_next);
        btnnext.setTag(mNext);
        if (TextUtils.isEmpty(mNext) || "null".equals(mNext)) {
            btnnext.setVisibility(View.INVISIBLE);
        } else {
            btnnext.setVisibility(View.VISIBLE);
        }
        final ProgressBar mLoading = (ProgressBar) mView.findViewById(R.id.mLoadding);
        final LineTimeBox timeBox = new LineTimeBox(mActivity,callIResult, mList, lineBoox, mDocterId,mData, mCancelDialog);
        timeBox.init(mCurrent);
        OnClickListener btnClick = new OnClickListener() {

            @Override
            public void onClick(final View v) {
                String date = (String) v.getTag();
                if (TextUtils.isEmpty(date)) {
                    return;
                }
                mLoading.setVisibility(View.VISIBLE);
                Utils.disableEnableControls(false, mView);

                ServerController.getInstance(mActivity).requestSearchDoctorSchedule(mDocterId, date,
                        new OnRequest<DoctorScheduleResponse>() {

                            @Override
                            public void onSuccess(DoctorScheduleResponse data) {
                                mLoading.setVisibility(View.GONE);
                                Utils.disableEnableControls(true, mView);

                                if (data == null || data.getItems() == null || data.getItems().isEmpty()) {
                                    Utils.showMessage(mActivity, "Khong co lich kham");
                                } else {
                                    timeBox.setList(data.getItems());
                                    timeBox.init(data.getDate());
//                                    tvCurrentDate.setText(DateFormat.format("dd-MM-yyyy",
//                                            Utils.convertDate(data.getDate(), "yyyy-MM-dd")));
//                                    
                                    tvCurrentDate.setText(data.getShowdate());
                                    
                                    btnback.setTag(data.getBack());
                                    if (TextUtils.isEmpty(data.getBack()) || "null".equals(data.getBack())) {
                                        btnback.setVisibility(View.INVISIBLE);
                                    } else {
                                        btnback.setVisibility(View.VISIBLE);
                                    }
                                    btnnext.setTag(data.getNext());
                                    if (TextUtils.isEmpty(data.getNext()) || "null".equals(data.getNext())) {
                                        btnnext.setVisibility(View.INVISIBLE);
                                    } else {
                                        btnnext.setVisibility(View.VISIBLE);
                                    }
                                }
                            }

                            @Override
                            public void onError(int errorCode, String message) {
                                mLoading.setVisibility(View.GONE);
                                Utils.disableEnableControls(true, mView);

                                Utils.showMessage(mActivity, message);
                            }
                        });
            }
        };
        btnback.setOnClickListener(btnClick);
        btnnext.setOnClickListener(btnClick);
    }
}
